package com.mjk.manager.api.work;


import com.mjk.common.base.error.BeeError;
import com.mjk.common.base.response.BeeResponse;
import com.mjk.common.base.work.BaseWork;
import com.mjk.common.base.work.annotation.BeeWork;
import com.mjk.common.core.jdbc.service.plus.idbservice.BeeDBService;
import com.mjk.common.core.jdbc.service.plus.wrap.BeeQueryWrapper;
import com.mjk.common.util.ObjectUtils;
import com.mjk.manager.api.dto.ApiDto;
import com.mjk.manager.api.interceptor.ApiParamsConverInterceptor;
import com.mjk.manager.api.model.ApiModel;
import com.mjk.manager.api.model.ApiParamsConver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.List;

/**
 * 版本 参数映射修改 如
 * v1 patName  name
 * v1 patName  name1
 * @author Administrator
 *
 */
@BeeWork(work="apiparams")
public class ApiParamsWork extends BaseWork {
	@Autowired
	private BeeDBService beeDBService;
	
	@Autowired
	private ApiParamsConverInterceptor paramsInterceptor;
	/**
	 * 获取出入参列表
     */
	public BeeResponse getApiParamsList(ApiDto dto){
		needParams(dto, ApiDto::getApiId);
		BeeQueryWrapper wrapper = new BeeQueryWrapper(new ApiParamsConver());
		wrapper.eq(ApiParamsConver::getApiId,dto.getApiId());
		if(isPage(dto)) {
			wrapper.setPage(dto.getPageStart(),dto.getPageSize());
            return BeeError.BEE_SUC.tranceError(beeDBService.findManyByWrapperWithPage(wrapper));
		}else {
            return BeeError.BEE_SUC.tranceError(beeDBService.findManyByWrapper(wrapper));
		}
	}
	
	/**
	 *
     */
	@SuppressWarnings("unchecked")
	public BeeResponse updateApiParams(ApiParamsConver model){
//		needParams(model, ApiParamsConver::getId);
//		BeeQueryWrapper apiWrapper = new BeeQueryWrapper(new ApiModel());
//		apiWrapper.eq(ApiModel::getId,model.getId());

//        List<ApiModel> apis = beeDBService.findManyByWrapper(apiWrapper);
//		if(CollectionUtils.isEmpty(apis)){
//			return BeeError.BEE_FAILD.tranceError("");
//		}
//		ApiModel api = apis.get(0);
		BeeQueryWrapper converWrapper = new BeeQueryWrapper(model);
		if(ObjectUtils.isNotEmpty(model.getId())){
			converWrapper.eq(ApiParamsConver::getId,model.getId());
			beeDBService.update(converWrapper);
		}else{
			needParams(model, "mappingVersion");
			beeDBService.insert(model);
		}
		converWrapper.eq(ApiParamsConver::getId,model.getId());
        List<ApiParamsConver> temConvers = beeDBService.findManyByWrapper(converWrapper);
		if(CollectionUtils.isEmpty(temConvers)){
			return BeeError.BEE_FAILD.tranceError("");
		}
		//更新拦截链中的数据
//		paramsInterceptor.maps.put(model.getMappingVersion()+api.getApiWork()+api.getApiMethod(),temConvers.get(0));
		return BeeError.BEE_SUC.tranceError("");
	}

	public BeeResponse delApiParams(ApiDto dto){
		needParams(dto,ApiParamsConver::getId);
		BeeQueryWrapper wrapper = new BeeQueryWrapper(new ApiParamsConver());
		wrapper.in(ApiParamsConver::getId, Arrays.asList(dto.getId().split(",")));
		beeDBService.delete(wrapper);
		return BeeError.BEE_SUC.tranceError("");
	}
}
